﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Xml;

namespace EMRIF.WHJXZXYJHYY
{
    public class WebServiceHelper
    {
        /// <summary>
        /// webservcie调用
        /// </summary>
        /// <param name="webServiceUrl">服务地址</param>
        /// <param name="methodName">方法名</param>
        /// <param name="paramList">参数列表</param>
        /// <param name="response">调用成功时返回服务参数,调用失败时返回报错信息</param>
        /// <returns>服务调用是否成功</returns>
        public static bool CallWebService(string webServiceUrl, string methodName, Dictionary<string, string> paramList, out string response)
        {
            response = "";
            bool flag = false;

            try
            {
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(webServiceUrl);
                webRequest.Method = "POST";
                //soap1.2对应的ContentType
                webRequest.ContentType = "application/soap+xml";
                byte[] data = GetRequestData(methodName, paramList);
                Stream requestStream = webRequest.GetRequestStream();
                requestStream.Write(data, 0, data.Length);
                requestStream.Flush();
                requestStream.Close();

                //返回消息
                HttpWebResponse httpWebResponse = webRequest.GetResponse() as HttpWebResponse;
                if (httpWebResponse.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    Stream responseStream = httpWebResponse.GetResponseStream();
                    StreamReader responseReader = new StreamReader(responseStream, Encoding.UTF8);
                    response = responseReader.ReadToEnd();
                    responseReader.Close();
                    responseStream.Close();
                    XmlDocument doc = new XmlDocument();
                    doc.LoadXml(response);
                    response = doc.DocumentElement.ChildNodes[0].ChildNodes[0].OuterXml;

                    flag = true;
                }
                else
                {
                    response = "服务连接失败:" + httpWebResponse.StatusCode;
                }
                //关闭
                httpWebResponse.Close();
            }
            catch (Exception ex)
            {
                response = ex.ToString();
            }

            return flag;
        }

        private static byte[] GetRequestData(string methodName, Dictionary<string, string> paramList)
        {
            StringBuilder requestData = new StringBuilder("<?xml version=\"1.0\" encoding=\"utf-8\"?>")
               .Append("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\">")
               .Append("<soap:Header/>")
               .Append("<soap:Body>")
               .Append($"<{methodName}>");
            foreach (KeyValuePair<string, string> parm in paramList)
            {
                requestData.AppendFormat("<{0}>{1}</{0}>", parm.Key, parm.Value);
            }
            requestData.Append($"</{methodName}>")
            .Append("</soap:Body>")
            .Append("</soap:Envelope>");
            string requestDataStr = requestData.ToString();
            return Encoding.UTF8.GetBytes(requestDataStr);
        }
    }
}
